package com.example.cleantest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;
import com.rapid.clean.deep.R;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class MemoryBoosterActivity extends Activity {

    private FrameLayout back;
    private ImageView out_booster_circle, out_booster_icons;
    private RelativeLayout relative_ad_23;


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_booster);
        initViews();
        show();

    }

    private void show() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startThirdActivity();
            }
        });
        startAnimation(3);

        showNativeAd();
    }

    private void showNativeAd() {
        try {
            AdSummary.getInstance().showNativeAd(this, relative_ad_23, false);
        } catch (Exception e) {
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateAd();

    }

    private void updateAd() {
        SharedPreferences sp_3 = getSharedPreferences("banner_ad", Context.MODE_PRIVATE);
        Long show_time_ad_3 = sp_3.getLong("show_banner_ad_time", 0L);

        if (show_time_ad_3 != 0L && (System.currentTimeMillis() - show_time_ad_3 >= (Integer.valueOf(Config.update_native_ad) * 1000))) {
            AdSummary.getInstance().showBannerAd(relative_ad_23);
        }
    }

    private void startAnimation(int useless) {
        Log.e("tg", "bn" + useless);

        RotateAnimation rotateAnimation = new RotateAnimation(0f, 1080f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(3000);
        //设置动画结束之后的状态是否是动画的最终状态，true，表示是保持动画结束时的最终状态
        rotateAnimation.setFillAfter(true);
        //设置动画结束之后的状态是否是动画开始时的状态，true，表示是保持动画开始时的状态
        rotateAnimation.setFillBefore(false);
        out_booster_circle.startAnimation(rotateAnimation);
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity();
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,0.0f,1.0f,0.0f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setDuration(3000);
        //设置动画结束之后的状态是否是动画的最终状态，true，表示是保持动画结束时的最终状态
        scaleAnimation.setFillAfter(true);
        //设置动画结束之后的状态是否是动画开始时的状态，true，表示是保持动画开始时的状态
        scaleAnimation.setFillBefore(false);
        out_booster_icons.startAnimation(scaleAnimation);

    }

    private void startActivity() {
        Intent intent = new Intent(MemoryBoosterActivity.this, MemoryBoosterResultActivity.class);
        startActivity(intent);
    }


    private void initViews() {
        back = (FrameLayout) findViewById(R.id.back);
        out_booster_icons = (ImageView) findViewById(R.id.out_booster_icons);
        out_booster_circle = (ImageView) findViewById(R.id.out_booster_circle);
        relative_ad_23 = (RelativeLayout) findViewById(R.id.relative_ad_23);
    }

    private void startThirdActivity() {
        Intent intent = new Intent(MemoryBoosterActivity.this, MemoryBoosterResultActivity.class);
        startActivity(intent);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        startThirdActivity();
    }

}
